var prefix = api + "/admin/statistic";

var dataTwo;

$(function () {
  one()
  setTimeout("two()", 500);
  setTimeout("three()", 1000);
});

function one() {
  var options = {
    id: "bootstrap-table-one",
    method: "get",
    // 没有分页.
    restype: 'data',
    pagination: false,
    url: prefix + "/nacosClientList",
    columns: [
      {
        title: "序号",
        align: 'center',
        formatter: function (value, row, index) {
          return $.table.serialNumber(index);
        }
      },
      {
        field: 'serviceId',
        title: '服务名',
        align: 'center',
      },
      {
        field: 'host',
        title: 'IP地址',
        align: 'center',
      },
      {
        field: 'port',
        title: '端口',
        align: 'center',
      },
      {
        field: 'healthy',
        title: '健康状态',
        align: 'center',
      },
      {
        field: 'scaName',
        title: '平台名称',
        align: 'center',
      }]
  };
  $.table.init(options);
  $.table.refresh()
}

function two() {
  renderStatisticsFilter('#serviceId')
  var options_2 = {
    id: "bootstrap-table-two",
    // 没有分页.
    restype: 'data',
    method: 'post',
    pagination: false,
    url: prefix + "/scaTcptList",
    columns: [
      {
        title: "序号",
        align: 'center',
        formatter: function (value, row, index) {
          return $.table.serialNumber(index);
        }
      },
      {
        field: 'artifactId',
        title: '组件标识',
        align: 'center',
        width: 200
      },
      {
        field: 'name',
        title: '中文名称',
        align: 'center',
        width: 200
      },
      {
        field: 'desc',
        title: '组件描述',
        align: 'center',
      }],
    onLoadSuccess: function (data) {
      dataTwo = data;
    }
  };
  $.table.init(options_2);
  $.table.refresh()
}

function three() {
  var options_3 = {
    id: "bootstrap-table-three",
    url: prefix + "/ideTcptList",
    columns: [
      {
        title: "序号",
        align: 'center',
        formatter: function (value, row, index) {
          return $.table.serialNumber(index);
        }
      },
      {
        field: 'appName',
        title: '应用名称',
        align: 'center',
      },
      {
        field: 'appDesc',
        title: '应用描述',
        align: 'center',
      },
      {
        field: 'componentName',
        title: '组件名称',
        align: 'center',
      },
      {
        field: 'componentLevel',
        title: '组件级别',
        align: 'center',
        formatter: function (value, row, index) {
          if (value == 0) {
            return "平台"
          }
          if (value == 1) {
            return "银行"
          }
          if (value == 2) {
            return "项目"
          }
          if (value == 9) {
            return "系统内置"
          }
        }
      },
      {
        field: 'count',
        title: '数量',
        align: 'center',
      },
      {
        field: 'createTime',
        title: '上报时间',
        align: 'center',
      }]
  };
  $.table.init(options_3);
  $.table.refresh()
}

function statisticsByType() {
  var result = []
  $.ajax({
    url: api + '/admin/statistic/nacosClientList',
    method: 'post',
    async: false
  }).done(function (res) {
    result = res.data
  })
  return result
}

function renderStatisticsFilter(obj) {
  var dict = statisticsByType()
  dict.forEach(function (item) {
    if (item.serviceId == "sca-upms-biz") {
      $(obj).append('<option selected = "selected" value="' + item.serviceId + '">' + item.serviceId + '</option>')
    } else {
      $(obj).append('<option value="' + item.serviceId + '">' + item.serviceId + '</option>')
    }
  })
}
var suffix="?Authorization="+'Bearer ' + session.get('token')+"&TENANT-ID="+session.get('tenantId');

function downloadFile() {
  if (!dataTwo.rows || !dataTwo.rows.length) {
    $.modal.alertError("SCA技术组件列表没有可导出的数据！");
    return false;
  }
   var serviceName = $("#serviceId").val();
   $.modal.confirm("确定要导出吗？", function() {
		location.href =  api + '/admin/statistic/doExport/'+serviceName+suffix;
		layer.msg('执行成功,正在后台统计请稍后…', {
			icon: 1
		});
	})


/*  var url = api + '/admin/statistic/doExport'
  var data = {server: serviceName}
  $.table.downloadFile(url, data, '组件统计汇总')*/
}

